package version1

import (
	"fmt"
	"server/global"
	entry "server/model/entrance"
	operatemodel "server/model/operate"
	"server/myutils"
	"strconv"

	"github.com/gin-gonic/gin"
)

//@Summary 获取所有的管理员数据
//@Router /v1/getalladmin [get]
//@Tags Gets
//@Description 需要权限为超级管理员权限才可获取
//@Produce json
//@Accept json
//@Success 200 {object} model.StandardResponse{} `接口被正确调用,但是调用结果看status状态码`
func GetAllAdmin(req *gin.Context) {
	data, code := operatemodel.Getalladmin()
	if code != global.SUCCESS {
		myutils.ResultJSON(req, code, nil)
		return
	}
	myutils.ResultJSON(req, code, data)
}

func DeleteAdmin(req *gin.Context) {
	s := req.Param("id")
	i, err := strconv.Atoi(s)
	if err != nil {
		myutils.ResultJSON(req, global.ERROR_FORMAT, nil)
		return
	}
	myutils.ResultJSON(req, operatemodel.Deleteadmin(i), nil)
}

//@Summary 添加管理员
//@Router /v1/addadmin [post]
//@Param data body entry.ADDAdmin true "参数"
//@Tags Posts
//@Description 需要超级管理员才能添加
//@Produce json
//@Accept json
//@Success 200 {object} model.StandardResponse{} `接口被正确调用,但是调用结果看status状态码`
func AddAdmin(req *gin.Context) {
	var bind = new(entry.ADDAdmin)
	if req.ShouldBind(bind) != nil {
		fmt.Println("123")
		myutils.ResultJSON(req, global.ERROR_FORMAT, nil)
		return
	}
	i, err := strconv.Atoi(bind.Role)
	if err != nil {

		myutils.ResultJSON(req, global.ERROR_FORMAT, nil)
	}
	var code = operatemodel.Addadmin(i, bind.Username, bind.Password)
	myutils.ResultJSON(req, code, nil)
}

//@Summary 更新管理员数据
//@Router /v1/updataadmin [put]
//@Param data body entry.UPDAdmin true  "参数"
//@Tags Updatas
//@Description 需要超级管理员权限
//@Produce json
//@Accept json
//@Success 200 {object} model.StandardResponse{} `接口被正确调用,但是调用结果看status状态码`
func UpdataAdmin(req *gin.Context) {
	var bind = new(entry.UPDAdmin)
	fmt.Println(bind)
	if req.ShouldBindJSON(bind) != nil {
		myutils.ResultJSON(req, global.ERROR_FORMAT, nil)
		return
	}
	fmt.Println("1")
	fmt.Println("2")
	role, err2 := strconv.Atoi(bind.Role)
	if err2 != nil {
		myutils.ResultJSON(req, global.ERROR_FORMAT, nil)
	}
	fmt.Println("3")
	fmt.Println(bind)
	myutils.ResultJSON(req, operatemodel.Updataadmin(role, bind.ID, bind.Username, bind.Password), nil)
}
